package net.gzw1993.runmate.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnRadioGroupCheckedChange;

import net.gzw1993.runmate.R;
import net.gzw1993.runmate.fragment.ChanllegeFragment;
import net.gzw1993.runmate.fragment.ChatFrendsFragment;
import net.gzw1993.runmate.fragment.MineFragment;
import net.gzw1993.runmate.fragment.RunGroupFragment;
import net.gzw1993.runmate.fragment.SportFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    @ViewInject(R.id.main_rg_group)
    private RadioGroup main_rg_group;
    private List<Fragment> fragments = new ArrayList<>();
    private ActionBar actionBar;
    private String[] title = {"跑团", "聊天", "运动", "活动", "个人主页"};
    private int fragmentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewUtils.inject(this, this);
        initView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.menu_main, menu);

        //menu.setGroupVisible(R.id.group_mine, false);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        actionBar = getSupportActionBar();
        initFragment();
        ((RadioButton) main_rg_group.getChildAt(0)).setChecked(true);
    }

    @OnRadioGroupCheckedChange(R.id.main_rg_group)
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < fragments.size(); i++) {
            // 设置Fragment的隐藏和显示
            RadioButton radioButton = (RadioButton) main_rg_group.getChildAt(i);
            ft.hide(fragments.get(i));
            if (radioButton.isChecked()) {
                fragmentIndex = i;

                // 设置ActionBar的状态
                if (i == 0) {
                    actionBar.setTitle("跑团");
                    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

                    //menu.setGroupVisible(R.id.group_mine, false);

                }
                if (i == 1) {
                    actionBar.setTitle("聊天");
                    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
                    //menu.clear();
                }
                if (i == 2) {
                    actionBar.setTitle("运动");
                    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
                    //menu.clear();
                }
                if (i == 3) {
                    actionBar.setTitle("活动");
                    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                    //menu.clear();
                }
                if (i == 4) {
                    actionBar.setTitle("个人主页");

                    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
                    // menu.clear();
                }
                if (!(fragments.get(i).isAdded())) {
                    ft.add(R.id.main_fl_container, fragments.get(i));
                    //menu.setGroupVisible(R.id.group_group,false);
                }
                ft.show(fragments.get(i));
            }

        }
        ft.commit();

    }

    /**
     * 初始化Fragment
     */
    public void initFragment() {
        fragments.add(RunGroupFragment.newInstance());
        fragments.add(ChatFrendsFragment.newInstance());
        fragments.add(SportFragment.newInstance());
        fragments.add(ChanllegeFragment.newInstance());
        fragments.add(MineFragment.newInstance());
    }
}
